package org.example.servicestudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.model.manage.bean.po.Student;
import org.example.servicestudent.mapper.StudentMapper;
import org.example.servicestudent.service.StudentService;
import org.example.servicestudent.util.LocationUtil;
import org.example.servicestudent.vo.StudentInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-07-16
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    /**
     * 添加学生信息
     * @param student 学生信息对象
     * @return 添加后的学生信息
     */
    @Override
    public Student addStudent(Student student) {
        // 检查学生对象是否为空
        if (student == null) {
            throw new IllegalArgumentException("学生信息不能为空");
        }
        
        // 保存学生信息
        studentMapper.insert(student);
        
        // 返回添加后的学生信息
        return student;
    }

    @Override
    public List<Student> getList(QueryWrapper<Student> unionId) {
        return studentMapper.selectList(unionId);
    }

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student binding(Long studentId, String unionId) {
        Student student = studentMapper.selectById(studentId);
        if (student != null) {
            student.setUnionId(unionId);
            studentMapper.updateById(student);
            return student;
        }
        return null;
    }

    @Override
    public Boolean positioning(String unionId, Double latitude, Double longitude) {
        if(LocationUtil.isWithinRange(latitude, longitude)) {
            studentMapper.update(new Student().setUnionId(unionId).setReportCheck(1), new QueryWrapper<Student>().eq("union_id", unionId));
            return true;
        }
        return false;
    }
    
    @Override
    public List<StudentInfoVO> getStudentInfoWithMajorName(String unionId) {
        return studentMapper.selectStudentInfoByUnionId(unionId);
    }
}
